<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="kuakao" uri="http://www.kuakao.net/tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>添加 / 修改</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/style.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/tool/jquery/date/date_input.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/tool/jquery/jqueryui/css/jquery.ui.all.css" />
	<script language="javascript" src="${pageContext.request.contextPath}/tool/jquery/jquery.min.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/tool/jquery/date/jquery.date_input.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/tool/jquery/jqueryui/jquery.ui.core.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/tool/jquery/jqueryui/jquery.ui.widget.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/tool/jquery/jqueryui/jquery.ui.position.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/tool/jquery/jqueryui/jquery.ui.autocomplete.js"></script>
	<script type="text/javascript">
		
		var initCustomerInfo = function(custId){//确定客户时，初始化相应数据
			//alert("id==="+custId);
			$.get("${pageContext.request.contextPath}/admin/user/get_customer_info.action",{"customerId":custId},function(data){
				var jsonData = $.parseJSON(data);
				if(!!jsonData['errorMsg']) {
					alert(jsonData['errorMsg']);
				}else{
					//alert("ywyId=="++",ywyName==="+jsonData['ywyName']+",invoice==="+jsonData['invoice']);
					$("#ywyId").val(jsonData['ywyId']);
					$("#operator").val(jsonData['ywyName']);
					$("#invoice").val(jsonData['invoice']);
				}
			});
		};//end function
		
		//动态计数,当前最新编号
var currentNum = 1;
var addOneLocal = function(){//动态添加
	var tem = $("#div_batchinfo_model").html().replace(/\_lId_0/g,"_lId_"+currentNum);
	$("#batchinfo").append(tem);
	//自动填写(产品)
	var autoNum = currentNum;
	$("#product_name_lId_"+currentNum).autocomplete({
		source: "${pageContext.request.contextPath}/admin/product/search_product.action",
		minLength: 1,
		select: function( event, ui ) {
			$("#product_id_lId_"+autoNum).val(ui.item.id);
		}
	});
	//计数
	currentNum = currentNum + 1;
};//function end
	
var delOneLocal = function(id){//删除一个页面新增的产品信息
	$("#status"+id).val("0");
	$("#bi_add"+id).attr("style","display: none;");
};//function end

var delOneRemote = function(oneId){//删除一个数据库中的产品信息
	$.post('${pageContext.request.contextPath}/test/delOneProduct.action',{opId:oneId},function(data) {
				if(data == "suc"){
					$("#p_rid_"+oneId).attr("style","display: none;");
				}else{
					alert(data);
				}
			});
};//function end

var updateOneRemote = function(oneId){//修改一个数据库中的产品信息
	var btn = $("#btn_rId_"+oneId);
	if(btn.val()=="修改"){
		$("#p_rid_"+oneId+" .su_input_readonly").removeAttr("readonly").removeAttr("style");
		btn.val("保存");
	}else{
		$.post('${pageContext.request.contextPath}/test/updateOneProduct.action',$("#form_rId_"+oneId).serialize(),function(data) {
				if(data == "suc"){
					$("#p_rid_"+oneId+" .su_input_readonly").attr("readonly","readonly").attr("style","border:0px");
					btn.val("修改");
				}else{
					alert(data);
				}
			});
		
	}
};//function end

var doBatchInfo = function(){//将批量添加的信息转化为字符串
		var res = "";
		if(currentNum > 1){
			for(var i=1 ; i<currentNum ; i++){
				//判断有效
				var bi_status = $("#status_lId_"+i).val();
				if(bi_status != "1" && bi_status != 1)continue;
				if(res != "")res = res + ",";
				res = res + "{productId:"+$("#product_id_lId_"+i).val()+",num:"+$("#num_lId_"+i).val()+",opprice:"+$("#price_lId_"+i).val()+",custname:'"+$("#custname_lId_"+i).val()+"',packname:'"+$("#packname_lId_"+i).val()+"'}";
			}
		}
		//alert(res);
		$("#input_orderProducts").val(res);
		//alert("ok");
};//function end	

var beforeSubmit = function(){//提交数据
	//validate
	//update remote list
	$("#form_order input[name='btn_remote_update']").each(function(){
		if($(this).val()=="保存"){
			$(this).click();
		}
	});
	//
	doBatchInfo();
	$('#form_order').submit();
};//function end
		
var submitAndGoon = function(){//保存并继续
	//validate
	//update remote list
	$("#form_order input[name='btn_remote_update']").each(function(){
		if($(this).val()=="保存"){
			$(this).click();
		}
	});
	//
	doBatchInfo();
	$.post("${pageContext.request.contextPath}/admin/order/do_save_order_and_goon.action",$("#form_order").serialize(),function(data){
			if(data == "suc"){
				doFormEmpty("form_order");
				$("#orderId").val(0);
				if(currentNum > 1){//清除之前的产品信息
					for(var i=1 ; i<currentNum ; i++){
						delOneLocal("_lId_"+i);
					}
				}
				addOneLocal();
			}else{
				alert(data);
			}
	});
};//function end
		
		$(function(){
			//自动填写(客户)
			$( "#customer_name" ).autocomplete({
				source: "${pageContext.request.contextPath}/admin/user/search_customer.action",
				minLength: 1,
				select: function( event, ui ) {
					$("#customerId").val(ui.item.id);
					initCustomerInfo(ui.item.id);
				}
			});
			
			if(${empty products}){
				addOneLocal();
			}
	//所有只读input初始化样式
	$(".su_input_readonly").attr("readonly","readonly").attr("style","border:0px");
			
			
		});//end function
	</script>
</head>
<body>
 <div class="mainCon">
  <div class="mCon">
  <div class="mCon_tab">
   <ul>   
    <li><span class="mCon_1cuta">添加 / 修改订单信息</span></li>

   </ul>
  </div>
  <br/>
  <div class="scxz"><strong>订单-基本信息</strong></div>
  <form action="${pageContext.request.contextPath}/admin/order/do_save_order.action" method="post" id="form_order">
  <input type="hidden" name="id" id="orderId" value="${empty baseinfo ? 0 : baseinfo.id}" />
  <div class="inke_list">
   	<div class="left">
   	<div class="mCon_1t mCon_2t"><strong>客户：</strong></div>
    <div class="mCon_con mCon_con2 su_edit_input_width">
		<input type="text" name="customer_name" id="customer_name" value="${baseinfo.customerName}" class="mCon_int su_auto_complate" />
		<input type="hidden" name="customerId" id="customerId" value="${baseinfo.customerId}" />
	</div>
   	</div>
   	<div class="left">
          <div class="mCon_1t mCon_2t"><strong>业务员：</strong></div>
          <div class="mCon_con mCon_con2 su_edit_input_width">
			<select name="ywyId" class="mCon_int" id="ywyId">
              <option value="">请选择</option>
              <c:if test="${!empty ywyList}">
              <c:forEach items="${ywyList}" var="one">
              	<option value="${one.id}" <c:if test="${baseinfo.ywyId==one.id}">selected="selected"</c:if>>${one.ywyname}</option>
              </c:forEach>
              </c:if>
            </select>
		  </div>
        </div>
   </div>
   
   <div class="inke_list">
   	<div class="left">
   	<div class="mCon_1t mCon_2t"><strong>经办人：</strong></div>
    <div class="mCon_con mCon_con2 su_edit_input_width">
		<input type="text" name="operator" id="operator" value="${baseinfo.operator}" class="mCon_int" />
	</div>
   	</div>
   	
   	<div class="left">
   	<div class="mCon_1t mCon_2t"><strong>联系方式：</strong></div>
    <div class="mCon_con mCon_con2 su_edit_input_width">
		<input type="text" name="contact" id="contact" value="${baseinfo.contact}" class="mCon_int" />
	</div>
   	</div>
   	
   </div>
   
   <div class="inke_list">
   	<div class="left">
   	<div class="mCon_1t mCon_2t"><strong>是否开票：</strong></div>
    <div class="mCon_con mCon_con2 su_edit_input_width">
		<select name="invoice" id="invoice" class="mCon_int">
			<option value="1" <c:if test="${baseinfo.buy==1}">selected="selected"</c:if>>是</option>
			<option value="2" <c:if test="${baseinfo.buy==2}">selected="selected"</c:if>>否</option>
		</select>
	</div>
   	</div>
   	
   </div>
   
   
   <div class="inke_list">
    <div class="mCon_1t mCon_2t"><strong>描述：</strong></div>
    <div class="mCon_con mCon_con2">
    	<textarea rows="3" cols="100" name="odesc">${baseinfo.odesc}</textarea>
    </div>
   </div>
<input type="hidden" name="orderProducts" id="input_orderProducts" />
  </form>
  
  <div class="scxz"><strong>订单-产品信息</strong></div>
  <div id="div_batchinfo_model" style="display: none;">
<p class="inke_list" id="bi_add_lId_0">
<input type="hidden" id="status_lId_0" value="1" />
产品：<input type="text" id="product_name_lId_0" class="su_auto_complate" />
<input type="hidden" id="product_id_lId_0" />
数量：<input type="text" id="num_lId_0" />
单价：<input type="text" id="price_lId_0" />
<br/>
包装：<input type="text" id="packname_lId_0" />
客户产品：<input type="text" id="custname_lId_0" />
<a title="删除" href="javascript:delOneLocal('_lId_0');"><img class="admin_sq" src="${pageContext.request.contextPath}/images/b_drop.png" /></a>
</p>
</div>
<div id="batchinfo">
<c:if test="${!empty products}">
<c:forEach items="${products}" var="one">
<form id="form_rId_${one.id}">
<p id="p_rid_${one.id}">
<input type="hidden" name="id" value="${one.id}" />
productId: ${one.productId}
num: <input type="text" name="num" value="${one.num}" class="su_input_readonly" />
price: <input type="text" name="price" value="${one.price}" class="su_input_readonly" />
<input type="button" value="del" onclick="delOneRemote(${one.id});" />
<input type="button" value="修改" name="btn_remote_update" id="btn_rId_${one.id}" onclick="updateOneRemote(${one.id});" />
</p>
</form>
</c:forEach>
</c:if>
</div>
<input type="button" value="添加产品信息" onclick="addOneLocal()" />

  <div class="scxz">
      <input class="fx1" type="button" onclick="beforeSubmit();" value="保 存" />
      <input class="fx1" type="button" onclick="submitAndGoon();" value="保存并继续" />
      <input class="fx1" type="button" onclick="history.back(-1);" value="返 回" />
  </div>
  <div class="clear"></div>
  </div>
  <div class="clear"></div>
 </div>
</body>
</html>


  